		/**
		 *计算总金额
		 */
		function calculateTotalCost(){
			var totalCost=0;
			$("input:checkbox[name^='cart_item']").each(function(){
				var skuId = $(this).val();
				if(this.checked){//如果选中 
					var goodsNum = $("#goods_num"+skuId).val();
					var goodsPrice = $("#goods_price"+skuId).val();
					var goodsTtoal =parseInt(goodsPrice)*parseInt(goodsNum);
					$("#goods_total"+skuId).val("￥"+formatNumWith2DecimalPlaces(goodsTtoal));
					totalCost+=goodsTtoal;
				}else{
					$("goods_total"+skuId).val("￥0.00");
				}
			});
			return totalCost;
		}
		function showCalculateTotal(){
			$("#total_cost").text("￥"+formatNumWith2DecimalPlaces(calculateTotalCost()));
		}
		/**
		 *数量修改
		 */
		function setGoodsTtoal(_this){
			var spinnerInput = $(_this).parent().children("input");
			if(spinnerInput==null||spinnerInput.length==0){
				return;
			}
			var skuId = $(spinnerInput[0]).attr("id").replace("goods_num","");
			var goodsNum = $("#goods_num"+skuId).val();
			var goodsPrice = $("#goods_price"+skuId).val();
			var goodsTtoal =parseInt(goodsPrice)*parseInt(goodsNum);
			$("#num_span"+skuId).text($(spinnerInput[0]).val());
			$("#goods_total"+skuId).text("￥"+formatNumWith2DecimalPlaces(goodsTtoal));
			//doChangeNum($("#goods_id"+skuId).val(),skuId,goodsNum);
			var isBuy = $("#cart_item"+skuId).prop("checked") == true ?1:0;
			modifyCartItemsCookie(skuId, goodsNum,isBuy,2);
		}
		function doChangeNum(goodsId,skuId,num){
			$.ajax({
				url:appPath+'/cart/changeCartItemCount.html',
				type:'post',
				dataType:'json',
				data:{"goodsId":goodsId,"skuId":skuId,"num":num,"token":$.cookie("token")},
				success:function(msg){
					//donothing
				},
				error:function(msg){
					$.alert("系统异常!");
				}
			});
		}
		function checkHasAllChecked(){
			//判断每个仓库下面是否全选
			$(":checkbox[name='storage']").each(function(){
				var storageId = $(this).attr("id").replace("storage","");
				if($(":checked[name='cart_item"+storageId+"']").length == $(":checkbox[name='cart_item"+storageId+"']").length){
					$(this).prop("checked",true);
				}else{
					$(this).prop("checked",false);
				}	
			});
			//判断所有仓库是否全选
			if($(":checked[name='storage']").length == $(":checkbox[name='storage']").length){
				$("#all_check").prop("checked",true);
			}else{
				$("#all_check").prop("checked",false);
			}	
			showCalculateTotal();
		}
		function toGoodsDetail(_this,skuId){
			var url =appPath+"/"+skuId+".html";
//			$(_this).parent().attr("href",url);
			window.location=url;
		}
		function readyDo(){
			$(".spinner").each(function(){
				$(this).spinner({"value":$(this).val(),"min":1});
			});
			checkHasAllChecked();
			$(".spinner").change(function(){
				setGoodsTtoal(this);
				showCalculateTotal();
			});
			$(".spinner button").click(function(){
				setGoodsTtoal(this);
				showCalculateTotal();
			});
			$("input:checkbox[name^='cart_item']").click(function(){
				showCalculateTotal();
				// 判断该仓库是否全选
				var storageId = $(this).attr("name").replace("cart_item","");
				if(!this.chekced&&$("#storage"+storageId).prop("checked")){
					$("#storage"+storageId).prop("checked",false);
				}else{
					if($(":checked[name='"+$(this).attr("name")+"']").length == $(":checkbox[name='"+$(this).attr("name")+"']").length){
						$("#storage"+storageId).prop("checked",true);
					}				
				}
				//判断所有仓库是否全选
				if($(":checked[name='storage']").length == $(":checkbox[name='storage']").length){
					$("#all_check").prop("checked",true);
				}else{
					$("#all_check").prop("checked",false);
				}	
				//isBuy标识切换
				modifyCartItemsCookie($(this), null,null,4);
			});
			$("input[name='storage']").click(function(){
				var storageId = $(this).val();
				$("input[name='cart_item"+storageId+"']").prop("checked",$(this).prop("checked"));
				$("input[name='cart_item"+storageId+"']").each(function(){
					modifyCartItemsCookie($(this), null,null,4);
				});
				//判断是否全选，否 全选设为false，
				if($(":checked[name='storage']").length == $(":checkbox[name='storage']").length){
					$("#all_check").prop("checked",true);
				}else{
					$("#all_check").prop("checked",false);
				}				
				showCalculateTotal();
			});
			$("#all_check").click(function(){
				$("input[name='storage']").prop("checked",$(this).prop("checked"));
				$("input[name^='cart_item']").prop("checked",$(this).prop("checked"));
				$("input[name^='cart_item']").each(function(){
					modifyCartItemsCookie($(this), null,null,4);
				});
				showCalculateTotal();
			});
			//删除购物项
			$("#delCartItems").click(function(){
				var  skuIds = new Array();
				var i =0;
				var obj = $("input:checked[name^='cart_item']");
				if(obj==null||obj.length==0){
					$.alert("请至少勾选一件购物项删除！");
					return;
				}
				$("input:checked[name^='cart_item']").each(function(){
					var skuId = $(this).val();
					skuIds[i]=skuId;
					i++;
					$("#sku_div"+skuId).remove();
				});
				
				showCalculateTotal();
				$("div[id^='storage_div']").each(function(){
					var storageId = $(this).attr("id");
					//console.log("storageId>>>>"+storageId);
					storageId = storageId.replace("storage_div","");
					if($(":checkbox[name='cart_item"+storageId+"']").length==0){
						$(this).remove();
					}
				});
				if($("div[id^='storage_div']").length==0){//清空了所有购物项
					//TODO 显示空购物项提示
					$("#empty_tip_div").show();
					$("#tabm_div").hide();
					$("#all_check").prop("checked",false);
				}
				//删除
				modifyCartItemsCookie(skuIds, null,null,3);
				
			});
			//跳转结算页
			$("#to_preorder").click(function(){
				var url = appPath+"/order/preOrderInfo.html"
				if($(":checked[name^='cart_item']").length==0){
					$.alert("请至少选择一件购物项！");
					return false;
				}
				var cartItemsCookie = $.cookie("cartItems");
				if(cartItemsCookie==undefined||"undefined"==cartItemsCookie||cartItemsCookie==null){
					cartItemsCookie="";
				}
				var token = $.cookie("token");
				if(token==undefined||"undefined"==token||token==null){
					token="";
				}
				url+="?token="+token;
				$(this).attr("href",url);
			});
			//同步购物车
			var synchronizeCookieStr =$("#synchronizeCookieStr").val();
			//if(synchronizeCookieStr!=null&&synchronizeCookieStr!=""){
				setCartItemsCookie(synchronizeCookieStr);
			//}
		}
		/**
		 * 格式化数字
		 */
		function formatNumWith2DecimalPlaces(num){
			var str = num+"";
			if(str.match(/^\d{1,2}$/g)){
				str="00"+str;
			}
			str = str.substr(0,str.length-2)+"."+str.substr(str.length-2,2);
			return str.replace(/^0{2,}\./g,"0.");
		}
		/**
		 * 移除详情页返回标识
		 * @param url
		 */
		function takeAstroll(url){
			try{
				//sessionStorage.removeItem("detailBackFlag");
				window.sessionStorage.setItem("notDetailBackFlag","1");
			}catch(e){
				
			}
			window.location.href = appPath + url;
		}